﻿using _06_坦克大战_正式.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06_坦克大战_正式
{
    enum Tag
    {
        MyTank,
        EnemyTank
    }
    internal class Bullet:Movething
    {
        private Random r = new Random();
        public bool IsDestroy { get; set; }
        public Tag Tag { get; set; }
        public Bullet(int x, int y, int speed,Direction dir,Tag tag)
        {
            IsDestroy = false;
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            BitmapDown = Resources.BulletDown;
            BitmapLeft = Resources.BulletLeft;
            BitmapUp = Resources.BulletUp;
            BitmapRight = Resources.BulletRight;
            this.Dir = dir;
            this.Tag = tag;
            BitmapDown.MakeTransparent(Color.Black);
            BitmapUp.MakeTransparent(Color.Black);
            BitmapLeft.MakeTransparent(Color.Black);
            BitmapRight.MakeTransparent(Color.Black);
            this.X -= Wideth/2;
            this.Y -= Height/2;
        }

        private void Move()
        {

            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
                default:
                    break;
            }


        }
        private void MoveCheck()
        {

            #region 边界碰撞检测
            //检查窗体边界
            if (Dir == Direction.Up)
            {
                if (Y + Height/2 + 3 < 0)
                {
                    IsDestroy = true;
                    return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Height/2 - 3 > 450)
                {
                    IsDestroy = true;
                    return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X + Wideth/2 - 3 < 0)
                {
                    IsDestroy = true;
                    return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Height/2 + 3 > 450)
                {
                    IsDestroy = true;
                    return;
                }
            }
            #endregion

            //检查墙体
            Rectangle rect = GetRectangle();
            rect.X = X + Wideth / 2 - 3;
            rect.Y = Y + Height / 2 - 3;
            rect.Height = 3;
            rect.Width = 3;
            int xExplosion = this.X + Wideth / 2;
            int yExplosion = this.Y + Height / 2;

            NotMovething wall = null;
            if ((wall =  GameObjectManager.IsColliedeWall(rect)) != null)
            {
                IsDestroy = true;
                GameObjectManager.DestroyWall(wall);
                GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                SoundManager.Playblast();
                return;
            }
            else if (GameObjectManager.IsColliedeSteel(rect) != null)
            {
                IsDestroy = true;
                GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                return;
            }
            else if (GameObjectManager.IsColliedeBoss(rect))
            {
                IsDestroy = true;
                GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                GameFramwork.ChangetoGameOver();
                SoundManager.Playblast();
                return;
            }
            if (Tag == Tag.MyTank)
            {
                EnemtTank tank = null;
                if((tank = GameObjectManager.IsCollidedEnemyTank(rect)) != null)
                {
                    SoundManager.Playhit();
                    IsDestroy = true;
                    GameObjectManager.CreateExplosion(xExplosion, yExplosion);
                    GameObjectManager.DestroyTank(tank);
                    return; 
                }
            }else if(Tag == Tag.EnemyTank)
            {
                MyTank tank = null;
                if((tank = GameObjectManager.IsCollideMyTank(rect)) != null)
                {
                    SoundManager.Playblast();
                    IsDestroy =true;
                    GameObjectManager.CreateExplosion(xExplosion, yExplosion);

                    tank.TakeDamage();

                    return;
                }
            }

        }
        public override void Update()
        {
            MoveCheck();
            Move();
            base.Update();
        }

        public override void DrawSelf()
        {

            base.DrawSelf();
        }
    }
}
